#include "pluginutil.h"
#include <QStringList>
#include <QPluginLoader>
#include <QFile>
#include <QCoreApplication>

#include <QtGui/QGenericPluginFactory>
#include "utils/log.h"

QString PluginUtil::findTouchPlugin() {
    QString pluginPath;

    QStringList list = QCoreApplication::libraryPaths();
    for(int i=0; i<list.length(); i++) {
        QString path = list[i]+"/generic/libqtslibplugin.so";
        QFile f(path);
        if(f.exists()) {
            pluginPath = path;
            break;
        }
    }

    return pluginPath;
}

void PluginUtil::reloadTouchPlugin() {
    QString pluginPath = findTouchPlugin();

    if(!pluginPath.isNull() && !pluginPath.isEmpty()) {
        QPluginLoader loader(pluginPath);

        if(loader.isLoaded()) {
            LOGE("Descarregando plugin.");
            LOGE(loader.errorString().toUtf8());
//            if(!loader.errorString().isNull() && !loader.errorString().isEmpty()) {
                loader.unload();
//            }
        }
    }

    QGenericPluginFactory factory;
    factory.create("Tslib","");
}
